<?php
	require_once( '../_config/base.inc.php' );
	
	use \dayscript\ErrorManager;
	use \dayscript\Application;
	use \dayscript\xml\rpc\Client;
	use \dayscript\util\Debug;
	use \dayscript\util\Benchmark;
	use \dayscript\application\Module;
	use \dayscript\event\Event;
	
	Application::getInstance( BASE_DIR . '_config/tests.inc.php' );
	
	ErrorManager::setUproarMode( true ); // Not for production
	
	class MyModule extends Module	{
		public function init( )	{
			$this->version = '1.0.0.0';
			$this->log( 'Inicializando.' );
			return true;
		}
		
		public function createListeners( )	{
			Application::addEventListener( $this, Event::LOAD, array( $this, 'applicationLoad') );
			Application::addEventListener( $this, Event::START, array( $this, 'applicationStart') );
			Application::addEventListener( $this, Event::RENDER, array( $this, 'applicationRender') );
			Application::addEventListener( $this, Event::COMPLETE, array( $this, 'applicationComplete') );
			Application::addEventListener( $this, Event::UNLOAD, array( $this, 'applicationUnload') );
			Application::addEventListener( $this, Event::SHUTDOWN, array( $this, 'applicationShutdown') );
			
			Application::addEventListener( $this, Event::MODULE_LOAD, array( $this, 'applicationModuleLoad') );
			
			$this->dispatchEvent( new Event( Event::MODULE_LOAD ) );
		}
		public function applicationLoad( Event $event )	{
			$this->log( 'Application has been loaded.' );
		}
		public function applicationComplete( Event $event )	{
			$this->log( 'Application has been completed.' );
		}
		public function applicationStart( Event $event )	{
			$this->log( 'Application has been started.' );
		}
		public function applicationRender( Event $event )	{
			$this->log( 'Application has been rendered.' );
		}
		public function applicationUnload( Event $event )	{
			$this->log( 'Application has been unloaded.' );
		}
		public function applicationShutdown( Event $event )	{
			$this->log( 'Application shutdown.' );
		}
		public function applicationModuleLoad( Event $event )	{
			$this->log( 'Application module load: target:' . ( \is_object( $event->getTarget( ) ) ? \get_class( $event->getTarget( ) ) : $event->getTarget( ) ) . ' currentTarget: ' . $event->getCurrentTarget( )->getName( ) );
		}
		
		public function dependencyCheck( )	{
			$this->log( 'Dependencias.' );
			return true;
		}
		public function updatesCheck( )	{
			$this->log( 'Actualizaciones.' );
			return false;
		}
	}
	
	class MyHTMLModule extends MyModule	{
		public function createListeners( )	{
			parent::createListeners( );
			Application::addEventListener( $this, Event::RENDER_START, array( $this, 'applicationRenderStart') );
			Application::addEventListener( $this, Event::END, array( $this, 'applicationRenderEnd') );
		}
		public function applicationRenderStart( Event $event )	{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Event / Dispatcher</title>
	<link rel="stylesheet" type="text/css" media="all" href="../_resources/styles.css" />
</head>
<body>
<div style="position:absolute;left:0px;right:0px;bottom:0px;"><div><a href="#" onclick="var d=this.parentNode.nextSibling.style;d.display=(d.display='none'?'':'none');return false;">&laquo;&raquo;</a></div><div>Hola</div></div>
&laquo;&raquo;
<?php
		}
		public function applicationRenderEnd( Event $event )	{
?>
</body>
</html>
<?php
		}
	}
	
	Application::addModule( new MyHTMLModule( 'HTML' ) );
	
	$module = new MyModule( 'Test I' );
	Application::addModule( $module );

	$module2 = new MyModule( 'Test II' );
	Application::addModule( $module2 );
	
	Application::start( );
